package project.background.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import fld.background.service.UserService;
import fld.background.system.service.impl.BaseServiceImpl;
import fld.utils.Common;
import fld.utils.DataGrid;
import fld.utils.Page;
import fld.utils.Result;
import project.background.dto.LeavingMessageDto;
import project.background.entity.LeavingMessage;

/**
 * *客户留言
 * @author yc
 * 2019年1月11日
 */
@Service
public class LeavingMessageService extends BaseServiceImpl<LeavingMessage,Long>{
	
	@Autowired
	private UserService userService;
	
	private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	/**
	 * 查询分页
	 * @param tmap
	 * @return
	 */
	public DataGrid grid(Map<String,Object> tmap){
		if(!"".equals(Common.nullToEmpty(tmap.get("name")))){
			tmap.put("name","%"+tmap.get("name")+"%");
		}
		if(!"".equals(Common.nullToEmpty(tmap.get("phone")))){
			tmap.put("phone","%"+tmap.get("phone")+"%");
		}
		if(!"".equals(Common.nullToEmpty(tmap.get("createTimes")))){
			String createTimes = Common.nullToEmpty(tmap.get("createTimes"));
			try {
				Date createTimess = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(createTimes+" 00:00:00");
				tmap.put("createTimes",createTimess);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if(!"".equals(Common.nullToEmpty(tmap.get("createTimee")))){
			String createTimee = Common.nullToEmpty(tmap.get("createTimee"));
			try {
				Date createTimeee = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(createTimee+" 23:59:59");
				tmap.put("createTimee",createTimeee);
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		if("".equals(Common.nullToEmpty(tmap.get("companyId")))) {
			tmap.put("companyId", userService.getCurUser().getCompanyId());
		}else {
			tmap.put("companyId", Long.parseLong(tmap.get("companyId").toString()));
		}
		Page<LeavingMessage> page = queryForPage(LeavingMessageDto.class, tmap);
		List<Map<String,Object>> rows = new ArrayList<Map<String,Object>>();
		List<LeavingMessage> res = page.getRows();
		for (LeavingMessage t : res) {
			Map<String,Object> m = new HashMap<String,Object>();
			m.put("id", t.getId());
			m.put("createTime", null == t.getCreateTime()?"":sdf.format(t.getCreateTime()));
			m.put("name", t.getName());
			m.put("phone", t.getPhone());
			m.put("wechatNum", t.getWechatNum());
			m.put("content", t.getContent());
			m.put("sfhf", t.getSfhf());
			m.put("companyId", t.getCompanyId());
			rows.add(m);
		}
		DataGrid dg = new DataGrid();
		dg.setTotal(page.getTotal());
		dg.setRows(rows);
		dg.setTotalPage(page.getPageCount());
		return dg;
	}
	
	/**
	 * 获取详情
	 * @param id
	 * @return
	 */
	public Map<String,Object> getInfo(Long id){
		LeavingMessage t = getDao().get(id);
		Map<String,Object> m = new HashMap<String,Object>();
		if(t != null){
			m.put("id", t.getId());
			m.put("createTime", null == t.getCreateTime()?"":sdf.format(t.getCreateTime()));
			m.put("name", t.getName());
			m.put("phone", t.getPhone());
			m.put("wechatNum", t.getWechatNum());
			m.put("content", t.getContent());
			m.put("sfhf", t.getSfhf());
			m.put("companyId", t.getCompanyId());
		}
		return m;
	}
	
	/**
	 * 新增/修改
	 * @param t
	 */
	public Result saveObj(LeavingMessage t) {
		if("".equals(Common.nullToEmpty(t.getId()))){
			t.setSfhf("2");
			save(t);
		}
		return new Result(true);
	}
	
	/**
	 * 设置成已回复
	 * @param id
	 * @return
	 * 2019年3月16日
	 * @author yc
	 */
	public Result hasHf(Long id){
		LeavingMessage t = getDao().get(id);
		if(null != t) {
			t.setSfhf("1");
			save(t);
		}
		return new Result(true);
	}
	
}
